﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Configuration;
using System.Web.UI.WebControls;
using DAL = KC.DataAccess.MV;
using BLL = KC.Business.Web.MV8;

public partial class Stats : System.Web.UI.Page
{
	
	DAL.MV8DataContext dc = new DAL.MV8DataContext(ConfigurationManager.ConnectionStrings["MV8"].ConnectionString);
	protected void Page_Load(object sender, EventArgs e)
	{
		// Handle Monthly Hits
		using (DataTable dt = BLL.GetMonthlyHits()) {
			//dgHits.DataSource = dt;
			//dgHits.DataBind();

			for (int i = 0; i < dt.Rows.Count; i++) {
			    cTrafficByMonth.Compression = 100;
			    cTrafficByMonth.Series["Spiders"].Points.AddXY(i, dt.Rows[i]["Spiders"].ToString());
			    cTrafficByMonth.Series["Humans"].Points.AddXY(i, dt.Rows[i]["Humans"].ToString());
			    cTrafficByMonth.Series["Total"].Points.AddXY(i, dt.Rows[i]["Total Hits"].ToString());
				cTrafficByMonth.Series[0].Points[i].AxisLabel = DateTime.Parse(dt.Rows[i][0].ToString()).ToString("MM/yy"); }
		}


		dgReferrers.DataSource = BLL.GetRecentReferrers(500);
		dgReferrers.DataBind();

		dgSearches.DataSource = BLL.GetRecentSearches(100);
		dgSearches.DataBind();

		dgStats.DataSource = new List<dynamic>() {
			new { Stat = "Total logged Activities: ", Value = (from a in dc.Activities select a).Count().ToString("###,###,###") },
			new { Stat = "Total Banned Words: ", Value = (from b in dc.BannedWords select b).Count().ToString("###,###") },
			new { Stat = "Total Blacklisted IPs and Domains: ", Value = (from b in dc.Blacklists select b).Count().ToString("###,###")},
			new { Stat = "Total Categories: ", Value = (from c in dc.Categories select c).Count().ToString("###,###")},
			new { Stat = "Total Comments: ", Value = (from c in dc.Comments select c).Count().ToString("###,###")},
			new { Stat = "Total Errors: ", Value = (from er in dc.Errors select er).Count().ToString("###,###,###")},
			new { Stat = "Total Gallery Items: ", Value = (from g in dc.Galleries select g).Count().ToString("###,###,###")},
			new { Stat = "Total History Items: ", Value = (from h in dc.Histories select h).Count().ToString("###,###,###")},
			new { Stat = "Total IP Addresses: ", Value = (from i in dc.IPs select i).Count().ToString("###,###,###")},
			new { Stat = "Total Posts: ", Value = (from p in dc.Posts select p).Count().ToString("###,###,###")},
			new { Stat = "Total Search Results: ", Value = (from s in dc.SearchResults select s).Count().ToString("###,###,###")},
			new { Stat = "Total Tags: ", Value = (from t in dc.Tags select t).Count().ToString("###,###")},
			new { Stat = "Total Users: ", Value = (from u in dc.Users select u).Count().ToString() },
			new { Stat = "Total visits from your IP: ", Value = (
				from a in dc.Activities 
				where a.Session.IP.Address == Request.UserHostAddress
				select a)
				.Count().ToString("###,###,###") }
		};
		dgStats.DataBind();
	}
	protected void dgHits_PreRender(object sender, EventArgs e)
	{
		foreach (DataGridItem i in dgHits.Items) {
			i.Cells[0].Text = DateTime.Parse(i.Cells[0].Text).ToString("MMMM yyyy");

			int Total = int.Parse(i.Cells[1].Text),
				Spiders = int.Parse(i.Cells[2].Text),
				Humans = int.Parse(i.Cells[3].Text);

			i.Cells[1].Text = Total.ToString("###,###");
			i.Cells[2].Text = Spiders.ToString("###,###") + " (" + (((double)Spiders / (double)Total) * 100).ToString("###") + "%)";
			i.Cells[3].Text = Humans.ToString("###,###") + " (" + (((double)Humans / (double)Total) * 100).ToString("###") + "%)"; }
	}
	protected void dgSearches_ItemDataBound(object sender, DataGridItemEventArgs e)
	{
		if (sender == null || e == null) return; // not gonna happen
		if (e.Item.DataItem == null)
			return;
		e.Item.Cells[0].Text = e.Item.Cells[0].Text.ToLower();
		e.Item.Cells[0].Text = e.Item.Cells[0].Text[0].ToString().ToUpper() + e.Item.Cells[0].Text.Substring(1);
	}
	protected void dgReferrers_ItemDataBound(object sender, DataGridItemEventArgs e)
	{
		if (sender == null || e == null) return; // not gonna happen
		string txt = e.Item.Cells[0].Text.Trim();
		if (txt.Length > 5 && txt[2] == '.')
			txt = txt.Substring(3);
		e.Item.Cells[0].Text = txt;
	}

}